<template>
  <div class="container">
    <van-list
      v-model="loading"
      :finished="finished"
      finished-text="没有更多了"
      @load="onLoad"
    >
      <van-cell v-for="(searchResult,index) in searchResults" :key="index" :title="searchResult.title" />
    </van-list>
  </div>
</template>

<script>
import {getSearchResult} from '../../../api/user';
export default {
  name: "Result",
  created() {},
  data() {
    return {
       searchResults: [],
      loading: false,
      finished: false,
      page:1,
      per_page:10
    };
  },
  props: {
    searchText:{
      type:String
    }
  },
  methods: {
    async onLoad() {
      // 异步更新数据
      const {data} = await getSearchResult({
        page:this.page,//页码
        per_page:this.per_page,//每页大小
        q: this.searchText//搜索的字符
      })
      // console.log(data);
      this.searchResults.push(...data.data.results)
      this.loading =false
      if(this.searchResults.length===0){
        this.finished=true
      }else{
        this.page++
      }

      // setTimeout 仅做示例，真实场景中一般为 ajax 请求
    //   setTimeout(() => {
    //     for (let i = 0; i < 10; i++) {
    //       this.list.push(this.list.length + 1);
    //     }

    //     // 加载状态结束
    //     this.loading = false;

    //     // 数据全部加载完成
    //     if (this.list.length >= 40) {
    //       this.finished = true;
    //     }
    //   }, 1000);
    },
  },
};
</script>

<style lang="less" scoped>
.container{
  position: fixed;
  left: 0;
  right: 0;
  top: 54px;
  bottom: 0;
  overflow: scroll;
}
</style>
